//
//  HCLeftMenuCell.m
//  hcpro
//
//  Created by 戴旭 on 16/9/20.
//  Copyright © 2016年 hucai.com. All rights reserved.
//

#import "HCLeftMenuCell.h"

@interface HCLeftMenuCell()
/** 菜单图标 */
@property(nonatomic, strong) UIImageView *img_menuIcon;
/** 菜单标题 */
@property(nonatomic, strong) UILabel *lbl_menuTitle;
/** 菜单描述 */
@property(nonatomic, strong) UILabel *lbl_menuDetail;
/** 提醒图标 */
@property(nonatomic, strong) UIImageView *img_badgeIcon;
/** 默认提醒图标 */
@property(nonatomic, strong) UIView *v_defaultBadgeIcon;
/** 菜单箭头 */
@property(nonatomic, strong) UIImageView *img_arrow;
@end

@implementation HCLeftMenuCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubviews];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
//    __block CGFloat margin = 10;
    /** 菜单图标 */
    [self.img_menuIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(2 * HCMargin);
        make.left.equalTo(2 * HCMargin);
        make.centerY.equalTo(self.contentView);
    }];
    /** 菜单标题 */
    [self.lbl_menuTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.img_menuIcon.right).offset(HCMargin);
        make.centerY.equalTo(self.img_menuIcon);
    }];
    /** 菜单描述 */
    [self.lbl_menuDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lbl_menuTitle.right).offset(HCMargin);
        make.bottom.equalTo(self.lbl_menuTitle);
    }];
    /** 提醒图标 */
    [self.img_badgeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(6);
        make.centerY.equalTo(self.lbl_menuTitle.top);
        make.left.equalTo(self.lbl_menuTitle.right).offset(HCMargin * 0.4);
    }];
    /** 默认提醒图标 */
    [self.v_defaultBadgeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.img_badgeIcon);
    }];
    /** 菜单箭头 */
    [self.img_arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.right).offset(-HCMargin);
        make.centerY.equalTo(self.contentView);
        make.height.equalTo(12);
        make.width.equalTo(8);
    }];
}

/** 添加子控件 */
- (void) setupSubviews{
    [self.contentView addSubview:self.img_menuIcon];
    [self.contentView addSubview:self.lbl_menuTitle];
    [self.contentView addSubview:self.lbl_menuDetail];
    
    [self.contentView addSubview:self.img_badgeIcon];
    [self.contentView addSubview:self.v_defaultBadgeIcon];
    
    [self.contentView addSubview:self.img_arrow];
}

/** lazyload */
/** 菜单图标 */
-(UIImageView *)img_menuIcon{
    if (_img_menuIcon == nil) {
        _img_menuIcon = [[UIImageView alloc] init];
        _img_menuIcon.backgroundColor = [UIColor clearColor];
    }
    return _img_menuIcon;
}
/** 菜单标题 */
- (UILabel *)lbl_menuTitle{
    if (_lbl_menuTitle == nil) {
        _lbl_menuTitle = [[UILabel alloc] init];
    }
    return _lbl_menuTitle;
}
/** 菜单描述 */
- (UILabel *)lbl_menuDetail{
    if (_lbl_menuDetail == nil) {
        _lbl_menuDetail = [[UILabel alloc] init];
        _lbl_menuDetail.hidden = YES;
        _lbl_menuDetail.textColor = [UIColor redColor];
        _lbl_menuDetail.font = HCSmallFont;
    }
    return _lbl_menuDetail;
}
/** 提醒图标 */
- (UIImageView *)img_badgeIcon{
    if (_img_badgeIcon == nil) {
        _img_badgeIcon = [[UIImageView alloc] init];
//        _img_badgeIcon.backgroundColor = [UIColor clearColor];
        _img_badgeIcon.hidden = YES;
    }
    return _img_badgeIcon;
}
/** 默认提醒图标 */
- (UIView *)v_defaultBadgeIcon{
    if (_v_defaultBadgeIcon == nil) {
        _v_defaultBadgeIcon = [[UIView alloc] init];
        _v_defaultBadgeIcon.backgroundColor = [UIColor redColor];
        _v_defaultBadgeIcon.layer.cornerRadius = 3;
        [_v_defaultBadgeIcon.layer masksToBounds];
        _v_defaultBadgeIcon.hidden = YES;
    }
    return _v_defaultBadgeIcon;
}
/** 菜单箭头 */
- (UIImageView *)img_arrow{
    if (_img_arrow == nil) {
        _img_arrow = [[UIImageView alloc] init];
        _img_arrow.backgroundColor = [UIColor clearColor];
        _img_arrow.image = [UIImage imageNamed:@"ic_arrow_grey"];
    }
    return _img_arrow;
}

/** 设置菜单项 */
- (void) setMenuItem:(HCLeftMenuItem *)menuItem{
    if (menuItem == nil) {
        return;
    }
    
    self.img_menuIcon.image = [UIImage imageNamed:menuItem.menu_icon];
    self.lbl_menuTitle.text = menuItem.menu_name;
    if (menuItem.menu_detail != nil && ![menuItem.menu_detail isEqualToString:@""]) {
        self.lbl_menuDetail.text = menuItem.menu_detail;
        self.lbl_menuDetail.hidden = NO;
    }
    else{
        self.lbl_menuDetail.hidden = YES;
    }
    
    if (menuItem.menu_isNeedShowBadge) {
        if (menuItem.menu_badgeIcon != nil  && ![menuItem.menu_badgeIcon isEqualToString:@""]) {
            self.img_badgeIcon.image = [UIImage imageNamed:menuItem.menu_badgeIcon];
            self.v_defaultBadgeIcon.hidden = YES;
            self.img_badgeIcon.hidden = NO;
        }
        else{
            self.v_defaultBadgeIcon.hidden = NO;
            self.img_badgeIcon.hidden = YES;
        }
    }
    else{
        self.v_defaultBadgeIcon.hidden = YES;
        self.img_badgeIcon.hidden = YES;
    }
}
@end
